package com.bawei.week.repair

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_base.entity.RepairBeanItem
import com.bawei.lib_base.mvvm.BaseActivity
import com.bawei.lib_base.utils.Cons
import com.bawei.week.R
import com.bawei.week.adapter.OnItemClickListener
import com.bawei.week.adapter.RepairImgAdapter
import com.bawei.week.databinding.ActivityRepairAddBinding
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/RepairAddActivity")
@AndroidEntryPoint
class RepairAddActivity : BaseActivity<ActivityRepairAddBinding,RepairViewModel>() {
    val repairImgAdapter by lazy { RepairImgAdapter() }
    var area = "A栋一层"
    override fun initView(){
        val areaList = arrayOf("A栋一层", "B栋一层")
        val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areaList)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.repairAddLocation.adapter = areaAdapter

        binding.repairAddLocation.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                area= areaList[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }
        }

        binding.repairSubmit.setOnClickListener {
            val repairName = binding.repairAddName.text.toString()
            val contactMethod = binding.repairAddContactMethod.text.toString()
            val faultDescription = binding.repairAddFaultDescription.text.toString()

            val repairBeanItem = RepairBeanItem(
                contactMethod,
                faultDescription,
                "",
                1,
                "WX${System.currentTimeMillis()}",
                "待处理",
                "1",
                repairName,
                vm.getDataTime(System.currentTimeMillis()),
                SPUtils.getInstance().getInt(Cons.UID)
            )
            vm.getRepairAdd(repairBeanItem)
        }

        binding.repairAddFaultPhotos.setOnClickListener {
            if (repairImgAdapter.data.size < 4){
                PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            }else{
                ToastUtils.showShort("最多能传四张")
            }
        }

        binding.repairAddRv.apply {
            adapter = repairImgAdapter
            layoutManager = LinearLayoutManager(this@RepairAddActivity,LinearLayoutManager.HORIZONTAL,false)
        }
    }

    override fun initData() {
        vm.repairAddSuc.observe(this){
            ToastUtils.showShort("添加成功")
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK){
            val parcelable = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            repairImgAdapter.addData(parcelable?.uri!!)
            repairImgAdapter.notifyDataSetChanged()
        }
    }
}